/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controlstudent.android.app;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.controlstudent.android.app.entidades.Alumno;
import com.controlstudent.android.app.entidades.Apoderado;
import com.controlstudent.android.app.util.URLUtil;
import com.google.gson.reflect.TypeToken;
import com.nlcode.util.gsonxml.GsonXmlUtil;
import com.nlcode.util.gsonxml.ListContainer;
import com.stanfy.jsonxml.GsonXml;
import java.util.List;

/**
 *
 * @author Batkev
 */
public class MenuPrincipal extends ListActivity {

    private ListaAlumnosAdapter adapter;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.menu);

        Bundle objBundle = getIntent().getExtras();
        Apoderado apod = (Apoderado) objBundle.getSerializable("com.controlstudent.android.app.apoderado");

        String url = "http://192.168.1.33:8084/SCWebServices/webresources/studentcontrolwsport/alumnosxapod/?id="+apod.getApod_id();
        String xml = URLUtil.getContent(url);

        GsonXml gxml = GsonXmlUtil.getGsonXml();
        List<Alumno> alumnos = gxml.<ListContainer>fromXml(xml, new TypeToken<ListContainer<Alumno>>() {
        }.getType()).getList();

        adapter = new ListaAlumnosAdapter(this, alumnos);
        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        try {
            Alumno a = adapter.getItem(position);
            Intent i = new Intent(MenuPrincipal.this, MenuEstudiante.class);
            Bundle objBundle = new Bundle();
            objBundle.putSerializable("com.controlstudent.android.app.alumno",a);
            i.putExtras(objBundle);
            startActivity(i);

        } catch (Exception e) {
            Toast.makeText(MenuPrincipal.this, "Sin acceso a menu estudiante", Toast.LENGTH_LONG).show();
        }

    }
}
